package com.service.impl;

import com.dao.BuyerDao;
import com.dao.RedisDao;
import com.dto.BaseDto;
import com.dto.MessageEnum;
import com.entity.Buyer;
import com.entity.Orderinfo;
import com.page.PageInfo;
import com.service.BuyerService;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service("buyerService")
public class BuyerServiceImpl implements BuyerService {
    @Resource
    BuyerDao buyerDao;


    @Resource
    RedisDao redisDao;

    public Buyer getByBuid(String buid){
      return buyerDao.getByBuid(buid);
    }
    //业务逻辑层，根据业务逻辑确定返回给页面的消息和状态以及对象
    public BaseDto<Buyer> getAll(){
        List<Buyer> buyers = buyerDao.getAll();
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = buyers.size() > 0?MessageEnum.SELECT_ADMINBYPAGE_SUCCESS:MessageEnum.SELECT_ADMINBYPAGE_FAIL;
        BaseDto<Buyer> dto = new BaseDto<>(messageEnum,buyers);
        return dto;
    }

    public BaseDto<Buyer> getAllByPage(PageInfo pageInfo){
        List<Buyer> buyers = buyerDao.getAllByPage(pageInfo);
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = buyers.size() > 0?MessageEnum.SELECT_ADMINBYPAGE_SUCCESS:MessageEnum.SELECT_ADMINBYPAGE_FAIL;
        BaseDto<Buyer> dto = new BaseDto<>(messageEnum,buyers);
        dto.setObj(pageInfo);
        return dto;
    }


    public BaseDto<Buyer> searchNameByPage(PageInfo pageInfo,String adname){
        List<Buyer> buyers = buyerDao.searchNameByPage(pageInfo,"%"+adname+"%");
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = buyers.size() > 0?MessageEnum.SEARCHADMIN_SUCCESS:MessageEnum.SEARCHADMIN_FAIL;
        BaseDto<Buyer> dto = new BaseDto<>(messageEnum,buyers);
        dto.setObj(pageInfo);
        return dto;
    }

    /**
     * 根据主键查询
     * @param buid
     * @return
     */
    public BaseDto getBuyerByBuid(String buid){
        Buyer buyer = buyerDao.getBuyerByBuid(buid);
        MessageEnum messageEnum = Optional.ofNullable(buyer).isPresent()?MessageEnum.SELECT_ADMINBYPK_SUCCESS:MessageEnum.SELECT_ADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(buyer);
        return dto;
    }

    /**
     * 添加一个实体
     * @param buyer
     * @return
     */
    public BaseDto addBuyer(Buyer buyer){
        int result = buyerDao.addBuyer(buyer);
        MessageEnum messageEnum = result>0?MessageEnum.ADDADMIN_SUCCESS:MessageEnum.ADDADMIN_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(buyer);
        return dto;
    }
    /**
     * 修改实体
     */
    public BaseDto updateBuyer(Buyer buyer){
        int result = buyerDao.updateBuyer(buyer);
        MessageEnum messageEnum = result>0?MessageEnum.UPDATEADMINBYPK_SUCCESS:MessageEnum.UPDATEADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(buyer);
        return dto;
    }
    public BaseDto updateBuyerByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param buid
     * @return
     */
    public BaseDto deleteBuyerByPrimayKey(String buid){
        int result = buyerDao.deleteBuyerByPrimayKey(buid);
        MessageEnum messageEnum = result>0?MessageEnum.DELETEADMINBYPK_SUCCESS:MessageEnum.DELETEADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        return dto;
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteBuyerByExample(){
        return null;
    }

   public  BaseDto getByBuidWithOrderInfos(String buid){
       Buyer buyer = buyerDao.getByBuidWithOrderInfos(buid);
//通过模糊查询得到同一个buyer全部 订单的key
       Set<Serializable> keys =  redisDao.getKeys(buyer.getBuid()+"_*");
      List<Orderinfo> unPayOrderinfos = new ArrayList<Orderinfo>();
      for(Serializable key :keys) {//遍历每一个该买家的key 得到订单对象
          String keyStr = key.toString();

          Orderinfo orderinfo = (Orderinfo) redisDao.getObj(keyStr);
          unPayOrderinfos.add(orderinfo);
      }

      buyer.setUnPayOrderinfos(unPayOrderinfos);//从redis中查询出用户未支付的订单

       Optional opt = Optional.ofNullable(buyer);
      MessageEnum messageEnum =  opt.isPresent()?MessageEnum.DELETEADMINBYPK_SUCCESS:MessageEnum.DELETEADMINBYPK_FAIL;
      BaseDto dto = new BaseDto(messageEnum);
      dto.setT(buyer);
      return dto;
   }



    public void test(Integer test1,String test2){
        System.out.println(test1 + "" + test2);
    }

}
